/*
 * Created on Aug 21, 2008
 * (c) 2009 by Reach Unlimited Corporation
 */
package ro.letsdoitromania.forms;

import java.util.Vector;
import javax.microedition.lcdui.*;
import com.mgmaps.tools.UITools;
import com.mgmaps.utils.LS;
import ro.letsdoitromania.*;

/**
 * This class handles display of marker details.
 *
 * @author CristiS
 * @version 1.0
 */
public class PlaceDetails implements CommandListener, ItemCommandListener {
  private Form form;
  private LDIRPlace place;
  
  private static PlaceDetails instance;
  
  public static PlaceDetails getInstance() {
    if (instance == null)
      instance = new PlaceDetails();
    return instance;
  }
  
  private PlaceDetails() {
  }
  
  public Form getForm() {
    return form;
  }
  
  /**
   * Show the details for a marker.
   * 
   * @param marker the marker
   */
  public void show(final LDIRPlace p) {
    place = p;
    
    Item[] items = null;
    
    final Vector cmds = new Vector();
    cmds.addElement(Commands.instance.backCommand);

    final int type = place.getType();
    if (type == LDIRPlace.PT_TRASHPOINT) {
      TrashPointData tpd = place.getData();
      int numPhotos = place.getPhotos().length;
      items = tpd.buildItems(true, numPhotos, this);
      if (Settings.isBlackberry) {
        if (numPhotos >= 1)
          cmds.addElement(Commands.cinstance.photo1Command);
        if (numPhotos >= 2)
          cmds.addElement(Commands.cinstance.photo2Command);
      }
    }
    else if (type == LDIRPlace.PT_CLUSTER) {
      // TODO implement cluster details
    }
    
    final Command[] cmdArray = new Command[cmds.size()];
    for (int i=0; i<cmdArray.length; i++)
      cmdArray[i] = (Command) cmds.elementAt(i);

    form = UITools.buildForm(form, LS.get("ldir.view.title"), items,
        cmdArray, this); 
    
    Display.getDisplay(LDIRMidlet.instance).setCurrent(form);
  }

  /**
   * Called when a command is selected.
   * @param command command
   * @param displayable current displayable (this)
   * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
   */
  public void commandAction(Command cmd, Displayable disp) {
    if (cmd == Commands.instance.backCommand) {
      LDIRCanvas.cinstance.photoHidden();
      Display.getDisplay(LDIRMidlet.instance).setCurrent(LDIRCanvas.instance);
      form.deleteAll();
    }
    
    else if (cmd == Commands.cinstance.photo1Command)
      viewPhoto(1);
    else if (cmd == Commands.cinstance.photo2Command)
      viewPhoto(2);
  }

  public void commandAction(Command cmd, Item item) {
    if (cmd == Commands.cinstance.photo1CommandItem)
      viewPhoto(1);
    else if (cmd == Commands.cinstance.photo2CommandItem)
      viewPhoto(2);
  }
  
  private void viewPhoto(int num) {
    String url = Settings.PHOTO_DL_URL + "?id=" + place.getID() + "&pn=" + num;
    url += "&w=" + LDIRCanvas.instance.getWidth() + "&h=" + LDIRCanvas.instance.getHeight();
    Settings.dl.enqueue(url);
    
    LDIRCanvas.cinstance.photoShown();
  }
}
